package com.pshcp.tool;

import com.framework.base.BaseDTO;
import com.framework.base.PageList;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.pshcp.ao.expertdiagnosis.BindExpertDiagnosisSessionIdAO;
import com.pshcp.ao.expertdiagnosis.ExistExpertDiagnosisSessionAO;
import com.pshcp.ao.expertdiagnosis.ExpertDiagnosisPageAO;
import com.pshcp.ao.expertdiagnosis.SetExpertDiagnosisUnReadCountAO;
import com.pshcp.ao.expertdiagnosis.SyncExpertDiagnosisIMMsgAO;
import com.pshcp.doctor.ao.diagnosis.InitDoctorContactPatientAO;
import com.pshcp.domain.dto.OnlineSessionDTO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.OnlineSession;
import com.pshcp.domain.model.Patient;
import com.pshcp.vo.expertdiagnosis.ExistExpertDiagnosisSessionStatusVO;
import com.pshcp.vo.expertdiagnosis.ExistExpertDiagnosisSessionVO;
import com.pshcp.vo.expertdiagnosis.OnlineSessionVO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/12/10 20:39
 */
public class ExpertDiagnosisTool {


    public static OnlineSessionDTO buildGetExpertDiagnosisPage(ExpertDiagnosisPageAO expertDiagnosisPageAO) {
        BaseDTO baseDTO = CommonTool.buildBaseDTO(expertDiagnosisPageAO.getPageNum(), expertDiagnosisPageAO.getPageSize());
        OnlineSessionDTO onlineSessionDTO = new OnlineSessionDTO();
        onlineSessionDTO.setPageNum(baseDTO.getPageNum());
        onlineSessionDTO.setPageSize(baseDTO.getPageSize());
        onlineSessionDTO.setSearch(expertDiagnosisPageAO.getSearch());
        onlineSessionDTO.setSessionStatusFlag(expertDiagnosisPageAO.getType());
        if (expertDiagnosisPageAO.getType() == null) {
            onlineSessionDTO.setSessionStatusList(Arrays.asList(0, 1, 2, 3));
        } else if (expertDiagnosisPageAO.getType() == 0) {
            onlineSessionDTO.setSessionStatusList(Arrays.asList(0));
        } else if (expertDiagnosisPageAO.getType() == 1) {
            onlineSessionDTO.setSessionStatusList(Arrays.asList(1));
        } else if (expertDiagnosisPageAO.getType() == 2) {
            onlineSessionDTO.setSessionStatusList(Arrays.asList(2, 3));
        }
        // 0 医生 1 患者
        if (ReqComTool.getUserType() == 0) {
            onlineSessionDTO.setSource(0);
            onlineSessionDTO.setDoctorId(ReqComTool.getUserId());
            if (expertDiagnosisPageAO.getPatientId() != null) {
                onlineSessionDTO.setPatientId(expertDiagnosisPageAO.getPatientId());
            }
        } else if (ReqComTool.getUserType() == 1) {
            onlineSessionDTO.setSource(1);
            onlineSessionDTO.setPatientUserId(ReqComTool.getUserId());
        }
        onlineSessionDTO.setType(expertDiagnosisPageAO.getCategory() == null ? 0 : expertDiagnosisPageAO.getCategory());
        return onlineSessionDTO;
    }

    public static PageList<OnlineSessionVO> buildGetExpertDiagnosisPage(PageList<OnlineSession> pageList) {
        PageList<OnlineSessionVO> page = new PageList<>();
        page.setPages(pageList.getPages());
        page.setPageSize(pageList.getPageSize());
        page.setPageNum(pageList.getPageNum());
        page.setTotal(pageList.getTotal());
        List<OnlineSessionVO> list = new ArrayList<>();
        List<OnlineSession> items = pageList.getItems();
        for (OnlineSession item : items) {
            OnlineSessionVO onlineSessionVO = CommonTool.copyProperties(item, OnlineSessionVO.class);
            onlineSessionVO.setUnReadCount(ReqComTool.getUserType() == 0 ? item.getDoctorUnReadCount() : item.getPatientUnReadCount());
            list.add(onlineSessionVO);
        }
        page.setItems(list);
        return page;
    }

    public static OnlineSession buildReceptionExpertDiagnosis(OnlineSession onlineSession) {
        onlineSession.setSessionStatus(1);
        return onlineSession;
    }

    public static OnlineSession buildCancelExpertDiagnosis(OnlineSession onlineSession) {
        onlineSession.setSessionStatus(3);
        return onlineSession;
    }


    public static OnlineSession buildSetExpertDiagnosisUnReadCount(OnlineSession onlineSession, SetExpertDiagnosisUnReadCountAO setExpertDiagnosisUnReadCountAO) {
        if (ReqComTool.getUserType() == 0) {
            onlineSession.setDoctorUnReadCount(setExpertDiagnosisUnReadCountAO.getUnReadCount());
        }
        if (ReqComTool.getUserType() == 1) {
            onlineSession.setPatientUnReadCount(setExpertDiagnosisUnReadCountAO.getUnReadCount());
        }
        return onlineSession;
    }

    public static OnlineSession buildClearExpertDiagnosisUnReadCount(OnlineSession onlineSession) {
        if (ReqComTool.getUserType() == 0) {
            onlineSession.setDoctorUnReadCount(0L);
        }
        if (ReqComTool.getUserType() == 1) {
            onlineSession.setPatientUnReadCount(0L);
        }
        return onlineSession;
    }


    public static OnlineSession buildBindIMSessionId(OnlineSession onlineSession, BindExpertDiagnosisSessionIdAO bindExpertDiagnosisSessionIdAO) {
        onlineSession.setSessionId(bindExpertDiagnosisSessionIdAO.getSessionId());
        onlineSession.setBeginTime(bindExpertDiagnosisSessionIdAO.getBeginTime());
        return onlineSession;
    }

    public static OnlineSession buildCloseExpertDiagnosisIMSession(OnlineSession onlineSession) {
        onlineSession.setEndTime(System.currentTimeMillis());
        onlineSession.setSessionStatus(2);
        return onlineSession;
    }

    public static OnlineSession buildSyncExpertDiagnosisIMMsg(OnlineSession onlineSession, SyncExpertDiagnosisIMMsgAO syncExpertDiagnosisIMMsgAO) {
        onlineSession.setLastMsgType(syncExpertDiagnosisIMMsgAO.getLastMsgType());
        onlineSession.setLastMsgContent(syncExpertDiagnosisIMMsgAO.getLastMsgContent());
        return onlineSession;
    }

    public static OnlineSession buildDelExpertDiagnosis(OnlineSession onlineSession) {
        if (ReqComTool.getUserType() == 0) {
            onlineSession.setDoctorDelStatus(1);
        }
        if (ReqComTool.getUserType() == 1) {
            onlineSession.setPatientDelStatus(1);
        }
        return onlineSession;
    }

    public static ExistExpertDiagnosisSessionStatusVO buildGetExistExpertDiagnosisSession(ExistExpertDiagnosisSessionAO existExpertDiagnosisSessionAO, List<OnlineSession> list) {
        ExistExpertDiagnosisSessionStatusVO vo = new ExistExpertDiagnosisSessionStatusVO();
        vo.setStatus(0);
        if (CollectionUtils.isEmpty(list)) {
            return vo;
        }
        for (OnlineSession onlineSession : list) {
            if (ReqComTool.getUserType() == 0 && existExpertDiagnosisSessionAO.getPatientUserId().equals(onlineSession.getPatientUserId()) && existExpertDiagnosisSessionAO.getPatientId().equals(onlineSession.getPatientId())) {
                vo.setStatus(1);
                vo.setType(onlineSession.getType());
                vo.setPatientOnlineOrderId(onlineSession.getPatientOnlineOrderId());
                vo.setSessionStatus(onlineSession.getSessionStatus());
                return vo;
            }
            if (ReqComTool.getUserType() == 1 && existExpertDiagnosisSessionAO.getDoctorId().equals(onlineSession.getDoctorId()) && existExpertDiagnosisSessionAO.getPatientId().equals(onlineSession.getPatientId())) {
                vo.setStatus(1);
                vo.setType(onlineSession.getType());
                vo.setPatientOnlineOrderId(onlineSession.getPatientOnlineOrderId());
                vo.setSessionStatus(onlineSession.getSessionStatus());
                return vo;
            }
        }
        return vo;
    }


    public static OnlineSession buildInitDoctorContactPatient(Doctor doctor, Patient patient) {
        OnlineSession onlineSession = new OnlineSession();
        onlineSession.setId(SystemUtil.getId());
        onlineSession.setType(1);
        onlineSession.setPatientOnlineOrderId(onlineSession.getId());
        onlineSession.setDoctorId(doctor.getId());
        onlineSession.setDoctorName(doctor.getName());
        onlineSession.setPatientUserId(patient.getPatientUserId());
        onlineSession.setPatientId(patient.getId());
        onlineSession.setPatientName(patient.getName());
        onlineSession.setSessionStatus(1);
        onlineSession.setPatientUnReadCount(0L);
        onlineSession.setDoctorUnReadCount(0L);
        onlineSession.setPatientDelStatus(0);
        onlineSession.setDoctorDelStatus(0);
        return onlineSession;
    }
}
